package com.amsp.oa.common.exception;

import com.amsp.oa.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice  //aop
@Slf4j
public class GlobalExceptionHandler {

    //全局异常处理
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R error(Exception e) {
        System.out.println("全局.....");
        log.error("全局.....{}", e.getMessage());
        e.printStackTrace();
        return R.error("执行全局异常处理");
    }

    //特定异常处理ArithmeticException
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R error(ArithmeticException e) {
        log.error("特定.....{}", e.getMessage());
        e.printStackTrace();
        return R.error("执行ArithmeticException异常处理");
    }

    //自定义异常处理GgktException
    @ExceptionHandler(OaException.class)
    @ResponseBody
    public R error(OaException e) {
        log.error("自定义.....{}", e.getMessage());
        return R.error(e.getCode(), e.getMsg());
    }

}
